package models

import (
	"context"
	"gorm.io/gorm"
	"major-six/major/goods/goods"
	"major-six/major/order/internal/svc"
)

type Goods struct {
	gorm.Model
	GoodsImage  string              `json:"goods_image" gorm:"type:varchar(255);"`
	GoodsName   string              `json:"goods_name" gorm:"type:varchar(20);"`
	GoodsPrice  float64             `json:"goods_price" gorm:"type:decimal(10,2);"`
	GoodsNum    int                 `json:"goods_num" gorm:"type:int(11);"`
	GoodsStock  int                 `json:"goods_stock" gorm:"type:int(11);"`
	GoodsStatus int                 `json:"goods_status" gorm:"type:tinyint(1);"`
	ctx         context.Context     `gorm:"all:-"`
	svcCtx      *svc.ServiceContext `gorm:"all:-"`
}

func NewGoodsModel(ctx context.Context, svcCtx *svc.ServiceContext) *Goods {
	return &Goods{
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (g *Goods) GoodsList() (info []*goods.GoodsInfo, err error) {
	return info, WithMysql(func(db *gorm.DB) error {
		return db.Table("goods").Find(&info).Error
	})
}

func (g *Goods) GoodsInfo(id int) (info *goods.GoodsInfo, err error) {
	return info, WithMysql(func(db *gorm.DB) error {
		return db.Table("goods").Where("id = ?", id).First(&info).Error
	})
}
